package com.example.myshixun.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myshixun.Myshixun4;
import com.example.myshixun.R;
import com.google.gson.Gson;

import org.json.JSONObject;

public class DetailActivity extends AppCompatActivity implements View.OnClickListener {
    TextView xqprice,xqfoodname;
    ImageView xqimage;
    EditText xqfood;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        initView();
        InitData();
    }

    private void InitData() {
        Intent intent = getIntent();
        int id = intent.getIntExtra("id",0);
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://10.20.64.6:8080/web_war_exploded/Menu/byId?id="+id;
        JSONObject jsonObject = new JSONObject();
        JsonObjectRequest jsonObjectRequest= new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Gson gson = new Gson();
                        FoodListBean.ListBean listBean = gson.fromJson(jsonObject.toString(), FoodListBean.ListBean.class);
                        xqfood.setText("名字：" + listBean.getName());
                        xqprice.setText("价格：" + listBean.getPrice() + "w");
                        xqfoodname.setText("介绍：" + listBean.getDescription());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(DetailActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue.add(jsonObjectRequest);
    }

    private void initView() {
    xqfood = findViewById(R.id.xq_food);
    xqprice = findViewById(R.id.xq_price);
    xqfoodname = findViewById(R.id.xq_food_name);

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}